package com.jiaoyou.baojian.pay;

import android.app.Activity;
import android.content.Context;
import android.os.Message;

import com.alipay.sdk.app.PayTask;
import com.jiaoyou.baojian.BaojianApplication;
import com.jiaoyou.baojian.constvalue.ConstValue;
import com.jiaoyou.baojian.utils.Utils;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Callback;

/**
 * Created by DemonZYM on 17/5/19.
 */

public class PayHelper {
    public static boolean payWeixin(String prepayid){
        IWXAPI api = BaojianApplication.getInstance().getWXApi();
        PayReq request = new PayReq();
        request.appId = ConstValue.WEIXIN_APPID;
        request.partnerId = ConstValue.WEIXIN_PARTNERID;
        request.prepayId= prepayid;
        request.packageValue = "Sign=WXPay";
        request.nonceStr= Utils.randomString(32);
        request.timeStamp= "1398746574";
        String stringA = "appId=" + request.appId +
                "&partnerId=" + request.partnerId +
                "&prepayId=" + request.prepayId +
                "&packageValue=" + request.packageValue +
                "&nonceStr=" + request.nonceStr +
                "&timeStamp=" + request.timeStamp;
        String stringSignTemp = stringA + "&key=" + ConstValue.WEIXIN_SIGNKEY;
        request.sign = Utils.md5(stringSignTemp).toUpperCase();
        return api.sendReq(request);
    }

    public static void getPrePayId(String out_trade_no, int total_fee, String itemDesc, Callback cb){
        String baseUrl = "https://api.mch.weixin.qq.com/pay/unifiedorder";
        HashMap<String, String> params = new HashMap<>();
        params.put("appid", ConstValue.WEIXIN_APPID);
        params.put("mch_id", ConstValue.WEIXIN_PARTNERID);
        params.put("nonce_str", Utils.randomString(32));
        params.put("sign", "");
        params.put("body", itemDesc);
        params.put("out_trade_no", out_trade_no);
        params.put("total_fee", total_fee + "");
        params.put("out_trade_no", out_trade_no);
        params.put("spbill_create_ip", Utils.getIPAddress());
        params.put("notify_url", ConstValue.WEIXIN_NOTIFYURL);  //http://www.weixin.qq.com/wxpay/pay.php
        params.put("trade_type", "APP");
//        params.put("limit_pay", "no_credit");   //no_credit--指定不能使用信用卡支付

        Utils.doHttpGet(baseUrl, params, cb);
    }

    public static void payAli(final Activity activity, final String orderInfo){
        // 订单信息
        Runnable payRunnable = new Runnable() {
            @Override
            public void run() {
                PayTask alipay = new PayTask(activity);
                Map<String, String> map = alipay.payV2(orderInfo, true);

//                Message msg = new Message();
//                msg.what = SDK_PAY_FLAG;
//                msg.obj = result;
//                mHandler.sendMessage(msg);
            }
        };
        // 必须异步调用
        Thread payThread = new Thread(payRunnable);
        payThread.start();
    }
}
